﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;//Please add references
namespace Catering_Operation
{
	/// <summary>
	/// 数据访问类:Quantity
	/// </summary>
	public partial class Quantity
	{
		public Quantity()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "Quantity"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Quantity");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(Catering_Model.Quantity model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Quantity(");
			strSql.Append("id,FMID,Energy,Protein,Fat,CelluloseA,Retinol,CelluloseB1,CelluloseB2,Nicotinic,CelluloseC,Carotene,Calcium,Iron)");
			strSql.Append(" values (");
			strSql.Append("@id,@FMID,@Energy,@Protein,@Fat,@CelluloseA,@Retinol,@CelluloseB1,@CelluloseB2,@Nicotinic,@CelluloseC,@Carotene,@Calcium,@Iron)");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@FMID", SqlDbType.Int,4),
					new SqlParameter("@Energy", SqlDbType.VarChar,50),
					new SqlParameter("@Protein", SqlDbType.VarChar,50),
					new SqlParameter("@Fat", SqlDbType.VarChar,50),
					new SqlParameter("@CelluloseA", SqlDbType.VarChar,50),
					new SqlParameter("@Retinol", SqlDbType.VarChar,50),
					new SqlParameter("@CelluloseB1", SqlDbType.VarChar,50),
					new SqlParameter("@CelluloseB2", SqlDbType.VarChar,50),
					new SqlParameter("@Nicotinic", SqlDbType.VarChar,50),
					new SqlParameter("@CelluloseC", SqlDbType.VarChar,50),
					new SqlParameter("@Carotene", SqlDbType.VarChar,50),
					new SqlParameter("@Calcium", SqlDbType.VarChar,50),
					new SqlParameter("@Iron", SqlDbType.VarChar,1)};
			parameters[0].Value = model.id;
			parameters[1].Value = model.FMID;
			parameters[2].Value = model.Energy;
			parameters[3].Value = model.Protein;
			parameters[4].Value = model.Fat;
			parameters[5].Value = model.CelluloseA;
			parameters[6].Value = model.Retinol;
			parameters[7].Value = model.CelluloseB1;
			parameters[8].Value = model.CelluloseB2;
			parameters[9].Value = model.Nicotinic;
			parameters[10].Value = model.CelluloseC;
			parameters[11].Value = model.Carotene;
			parameters[12].Value = model.Calcium;
			parameters[13].Value = model.Iron;

			DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Catering_Model.Quantity model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Quantity set ");
			strSql.Append("FMID=@FMID,");
			strSql.Append("Energy=@Energy,");
			strSql.Append("Protein=@Protein,");
			strSql.Append("Fat=@Fat,");
			strSql.Append("CelluloseA=@CelluloseA,");
			strSql.Append("Retinol=@Retinol,");
			strSql.Append("CelluloseB1=@CelluloseB1,");
			strSql.Append("CelluloseB2=@CelluloseB2,");
			strSql.Append("Nicotinic=@Nicotinic,");
			strSql.Append("CelluloseC=@CelluloseC,");
			strSql.Append("Carotene=@Carotene,");
			strSql.Append("Calcium=@Calcium,");
			strSql.Append("Iron=@Iron");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@FMID", SqlDbType.Int,4),
					new SqlParameter("@Energy", SqlDbType.VarChar,50),
					new SqlParameter("@Protein", SqlDbType.VarChar,50),
					new SqlParameter("@Fat", SqlDbType.VarChar,50),
					new SqlParameter("@CelluloseA", SqlDbType.VarChar,50),
					new SqlParameter("@Retinol", SqlDbType.VarChar,50),
					new SqlParameter("@CelluloseB1", SqlDbType.VarChar,50),
					new SqlParameter("@CelluloseB2", SqlDbType.VarChar,50),
					new SqlParameter("@Nicotinic", SqlDbType.VarChar,50),
					new SqlParameter("@CelluloseC", SqlDbType.VarChar,50),
					new SqlParameter("@Carotene", SqlDbType.VarChar,50),
					new SqlParameter("@Calcium", SqlDbType.VarChar,50),
					new SqlParameter("@Iron", SqlDbType.VarChar,50),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.FMID;
			parameters[1].Value = model.Energy;
			parameters[2].Value = model.Protein;
			parameters[3].Value = model.Fat;
			parameters[4].Value = model.CelluloseA;
			parameters[5].Value = model.Retinol;
			parameters[6].Value = model.CelluloseB1;
			parameters[7].Value = model.CelluloseB2;
			parameters[8].Value = model.Nicotinic;
			parameters[9].Value = model.CelluloseC;
			parameters[10].Value = model.Carotene;
			parameters[11].Value = model.Calcium;
			parameters[12].Value = model.Iron;
			parameters[13].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Quantity ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Quantity ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Catering_Model.Quantity GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,FMID,Energy,Protein,Fat,CelluloseA,Retinol,CelluloseB1,CelluloseB2,Nicotinic,CelluloseC,Carotene,Calcium,Iron from Quantity ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = id;

			Catering_Model.Quantity model=new Catering_Model.Quantity();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["FMID"]!=null && ds.Tables[0].Rows[0]["FMID"].ToString()!="")
				{
					model.FMID=int.Parse(ds.Tables[0].Rows[0]["FMID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Energy"]!=null && ds.Tables[0].Rows[0]["Energy"].ToString()!="")
				{
					model.Energy=ds.Tables[0].Rows[0]["Energy"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Protein"]!=null && ds.Tables[0].Rows[0]["Protein"].ToString()!="")
				{
					model.Protein=ds.Tables[0].Rows[0]["Protein"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Fat"]!=null && ds.Tables[0].Rows[0]["Fat"].ToString()!="")
				{
					model.Fat=ds.Tables[0].Rows[0]["Fat"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CelluloseA"]!=null && ds.Tables[0].Rows[0]["CelluloseA"].ToString()!="")
				{
					model.CelluloseA=ds.Tables[0].Rows[0]["CelluloseA"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Retinol"]!=null && ds.Tables[0].Rows[0]["Retinol"].ToString()!="")
				{
					model.Retinol=ds.Tables[0].Rows[0]["Retinol"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CelluloseB1"]!=null && ds.Tables[0].Rows[0]["CelluloseB1"].ToString()!="")
				{
					model.CelluloseB1=ds.Tables[0].Rows[0]["CelluloseB1"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CelluloseB2"]!=null && ds.Tables[0].Rows[0]["CelluloseB2"].ToString()!="")
				{
					model.CelluloseB2=ds.Tables[0].Rows[0]["CelluloseB2"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Nicotinic"]!=null && ds.Tables[0].Rows[0]["Nicotinic"].ToString()!="")
				{
					model.Nicotinic=ds.Tables[0].Rows[0]["Nicotinic"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CelluloseC"]!=null && ds.Tables[0].Rows[0]["CelluloseC"].ToString()!="")
				{
					model.CelluloseC=ds.Tables[0].Rows[0]["CelluloseC"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Carotene"]!=null && ds.Tables[0].Rows[0]["Carotene"].ToString()!="")
				{
					model.Carotene=ds.Tables[0].Rows[0]["Carotene"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Calcium"]!=null && ds.Tables[0].Rows[0]["Calcium"].ToString()!="")
				{
					model.Calcium=ds.Tables[0].Rows[0]["Calcium"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Iron"]!=null && ds.Tables[0].Rows[0]["Iron"].ToString()!="")
				{
					model.Iron=ds.Tables[0].Rows[0]["Iron"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,FMID,Energy,Protein,Fat,CelluloseA,Retinol,CelluloseB1,CelluloseB2,Nicotinic,CelluloseC,Carotene,Calcium,Iron ");
			strSql.Append(" FROM Quantity ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,FMID,Energy,Protein,Fat,CelluloseA,Retinol,CelluloseB1,CelluloseB2,Nicotinic,CelluloseC,Carotene,Calcium,Iron ");
			strSql.Append(" FROM Quantity ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Quantity";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

